﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myTwitter.BO;
using System.Diagnostics;
using myBoard.Framework;
using System.Windows.Media.Animation;

namespace myTwitter.CutomControls
{
	
	public partial class UserInfoControl
	{
        public delegate void SeeUserTweetsRequestedHandler(User user);
        public delegate void SeeUserFollowersRequestedHandler(User user);
        public delegate void SeeUserFriendsRequestedHandler(User user);
        public event SeeUserTweetsRequestedHandler SeeUserTweetsRequested;
        public event SeeUserFollowersRequestedHandler SeeUserFollowersRequested;
        public event SeeUserFriendsRequestedHandler SeeUserFriendsRequested;


        private Storyboard _loadingOnSB;
        private Storyboard _loadingOffSB;

		public UserInfoControl()
		{
			this.InitializeComponent();
            _loadingOnSB = this.Resources["LoadingOn"] as Storyboard;
            _loadingOffSB = this.Resources["LoadingOff"] as Storyboard;
            _seeTweetsBt.Click += new RoutedEventHandler(SeeTweetsBt_Click);
            _seeFriendsBt.Click += new RoutedEventHandler(_seeFriendsBt_Click);
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(UserInfoControl_DataContextChanged);
		}        

        public void LoadingOn()
        {
            this.BeginStoryboard(_loadingOnSB);
            _preLoader.Start();
        }

        public void LoadingOff()
        {
            this.BeginStoryboard(_loadingOffSB);
            _preLoader.Stop();
        }

        void UserInfoControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null && e.NewValue is User)
            {
                User user = e.NewValue as User;
                if (string.IsNullOrEmpty(user.Url))
                    _websiteStack.Visibility = Visibility.Collapsed;
                else
                    _websiteStack.Visibility = Visibility.Visible;
                if (user.Relationship != null)
                {
                    if (user.Relationship.SourceFollowing)
                    {
                        _followMeBt.Visibility = Visibility.Collapsed;
                        _stopFollowMeBt.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        _followMeBt.Visibility = Visibility.Visible;
                        _stopFollowMeBt.Visibility = Visibility.Collapsed;
                    }
                }
            }
        }

        void SeeTweetsBt_Click(object sender, RoutedEventArgs e)
        {
            if (SeeUserTweetsRequested != null)
                SeeUserTweetsRequested(this.DataContext as User);
        }

		private void _visitWebSiteBt_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            User user = this.DataContext as User;
            if (!string.IsNullOrEmpty(user.Url))
            {
                Connector.HideMyBoard();
                Process.Start(user.Url);
            }
		}

        private void _visitTwitterPageBt_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            User user = this.DataContext as User;
            if (!string.IsNullOrEmpty(user.TwitterUrl))
            {
                Connector.HideMyBoard();
                Process.Start(user.TwitterUrl);
            }
        }

		private void _followMeBt_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			User user = this.DataContext as User;
            GlobalController.Instances.ITwitterProvider.Follow(user);
		}

		private void _stopFollowMeBt_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            User user = this.DataContext as User;
            GlobalController.Instances.ITwitterProvider.UnFollow(user);
		}

		private void _seeFollowersBt_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            if (SeeUserFollowersRequested != null)
                SeeUserFollowersRequested(this.DataContext as User);
		}

        void _seeFriendsBt_Click(object sender, RoutedEventArgs e)
        {
            if (SeeUserFriendsRequested != null)
                SeeUserFriendsRequested(this.DataContext as User);
        }
	}
}